package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;

import csc.daonjpa.java.controller.EntityManagerFactoryUtil;
import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.transaction.TransactionManager;

@Component
public class CustomerDAO {
	/**
	 * @return all accounts from Account table
	 */
	public List<Customer> getCustomers() {
		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Customer> query = em.createQuery("SELECT a FROM Customer a", Customer.class);
		List<Customer> customers = null;
		customers = query.getResultList();
		return customers;
	}
	
	/**
	 * Add account to Account table
	 * @param account Account entity that contains mapped with each column in Account table
	 * @return
	 */
	public boolean addCustomer(Customer customer) {
		EntityManager em = TransactionManager.getEntityManager();
		em.persist(customer);
		return true;
	}
	/**
	 * get customer by loginId
	 * @param loginId
	 * @return
	 */
	public Customer getCustomer(String loginId) {
		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Customer> query = em.createQuery("SELECT c FROM Customer c WHERE c.loginId = :id", Customer.class);
		Customer customer = query.setParameter("id", loginId).getSingleResult();
		return customer;
	}
	/**
	 * Check when logging
	 * @param id
	 * @param pass
	 * @return
	 */
	public Integer checkLogin(String id, String pass) {
		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Customer> query = em.createQuery("SELECT c FROM Customer c WHERE c.loginId = :id AND c.password = :pass", Customer.class);
		return query.setParameter("id", id).setParameter("pass", pass).getResultList().size();
	}

	public boolean updateCustomer(Customer cus) {
		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		boolean res = true;
		try {
			entr.begin();
			String temp = "UPDATE " + Customer.class.getName()
					+ " SET password = :password, "
					+ " firstName = :firstName , lastName = :lastName, "
					+ " address= :address , phone = :phone " + "WHERE id = :id";
			Query query = entityManager.createQuery(temp);
			query.setParameter("id", cus.getId());
			query.setParameter("password", cus.getPassword());
			query.setParameter("firstName", cus.getFirstName());
			query.setParameter("lastName", cus.getLastName());
			query.setParameter("address", cus.getAddress());
			query.setParameter("phone", cus.getPhone());

			query.executeUpdate();
			entr.commit();
		} catch (Exception e) {
			entityManager.close();
			res = false;
		}
		// -----------End transaction-----------
		return res;
	}
}
